<?php

namespace app\proxy\model\store;

use app\common\model\store\Store as StoreModel;
use app\proxy\model\proxy\Region;

class Store extends StoreModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'app_id',
        'update_time',
    ];

    /**
     * 获取列表数据
     */
    public function getList($data = null, $status = '', $regionIds = [],$proxyId=0)
    {
        $model = $this;
        !empty($status) && $model = $model->where('store.status', '=', (int)$status);
        $data['check_store'] != '' && $model = $model->where('store.check_store', '=', $data['check_store']);
        $list = $model->alias("store")
            ->join("supplier supplier", "supplier.shop_supplier_id=store.shop_supplier_id", 'left')
            ->with(['logo'])
            ->where('store.is_delete', '=', '0')
            ->where(function ($query) use ($regionIds) {
                $query->whereIn('supplier.region_id', $regionIds)->whereOr("supplier.province_id", "in", $regionIds)->whereOr("supplier.city_id", "in", $regionIds);
            })
            ->withAttr('proxy_name', function ($value, $data) use ($proxyId) {
                return Region::getProxyName($data, $proxyId);
            })
            ->order(['store.sort' => 'asc', 'store.create_time' => 'desc'])
            ->field(["store.*", 'supplier.province_id', 'supplier.city_id', 'supplier.county_id', 'supplier.proxy_id','supplier.name supplier_name','supplier.region_name'])
            ->append(["proxy_name"])
            ->paginate($data);
        return $list;
    }

    /**
     * 获取所有门店列表
     */
    public static function getAllList()
    {
        return (new self)->where('is_delete', '=', '0')
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->select();
    }

}